<template>
  <DataList 
:cate="cate"
            ref="datalist"
            :cols="cols"
            :excelHeader="excelHeader"
            :groupCheck="groupCheck"
            :name="name"
            skipExcelNumber="3"><template slot="search">
              <el-form-item>
                <el-input 
v-model="search_str"
                          placeholder="请输入教师名字搜索"
                  @change="clear"
                          clearable
                  @input="mysearch" />
              </el-form-item>
  </template></DataList>
</template>
<script>
import DataList from '@/components/DataList'
import { getUser } from '@/utils/auth'
import { get教师其他情况E, update教师其他情况E, delete教师其他情况E, add教师其他情况E, bulkAddJsqtqkE } from '@/api/教师其他情况'
import { get教师基本情况ByGH, get教师基本情况 } from '@/api/教师基本情况'
import linq from 'linq'
export default {
  components: {
    DataList
  },
  data() {
    return {
      cate: 'A6教师技术专利情况',
      name: '6.3.2 技术专利',
      excelHeader: { B3: '教工号', C3: '姓名', D3: '获技术专利（技术发明）项目', D4: '获得日期（年月）', E4: '技术专利（发明）名称(全称)', F4: '技术专利（发明）编号', G4: '是否主持' },
      cols: [
        { label: '教工号', prop: 'GH' },
        { label: '姓名', prop: 'XM' },
        { label: '获技术专利(技术发明)项目', prop: '获技术专利技术发明项目',
          data:
            [
              { label: '获得日期(年月)', prop: 'JSZL_QDRQ' },
              { label: '技术专利(发明)名称', prop: 'JSZL_MC' },
              { label: '技术专利(发明)编号', prop: 'JSZL_BH' },
              { label: '是否主持', prop: 'XZ_SFZCZL' }
            ]
        }

      ],
      groupCheck: [
        { data: ['JSZL_QDRQ', 'JSZL_MC', 'JSZL_BH', 'XZ_SFZCZL'
        ], desc: '获技术专利【获得日期】等字段，如果填了其中1个，其他的必须填写！' }
      ]
    }
  },
  methods: {
    getSummaries(list) {
      //
      const sums = []
      this.cols.forEach((column, index) => {
        sums[index] = ''
      })
      sums[0] = '合计'
      sums[7] = '教师数:' + linq.from(list).distinct('$.GH').count()
      sums[9] = '项目数:' + linq.from(list).where('$.JSZL_MC').count()

      sums[11] = '主持:' + linq.from(list).where('$.XZ_SFZCZL == "是"').count()

      sums[12] = '通过' + linq.from(list).where('$.审核状态 =="通过"').count()
      sums[12] += ',不通过' + linq.from(list).where('$.审核状态 =="不通过"').count()
      sums[12] += ',待审核' + linq.from(list).where('$.审核状态 =="待审核"').count()
      return sums
      //
    },
  
    check(array, list) {
      return new Promise(async(resolve, reject) => {
        try {
          var user = getUser()
          var exists = await get教师其他情况E(user.XXID, user.CJPC)
  var 教工号 = await get教师基本情况(user.XXID,user.CJPC)
          // var duplicates = linq.from(array).groupBy('$.GH').where('$.count()>1').toArray()
          // duplicates.forEach(e => {
          //   linq.from(array).where(t => t.GH == e.key()).forEach(t => t.error = '录入数据存在相同的教工号')
          // })
          // // 是否已经存在数据
          var err = linq.from(array).select("$.GH+','+$.JSZL_BH").intersect(linq.from(exists).select("$.GH+','+$.JSZL_BH").toArray()).toArray()
          linq.from(array).where(e => err.indexOf(e.GH) !== -1).forEach(e => e.error = e.error ? e.error + ';' : '' + '数据库中已经存在相同的数据')
          // 是否已经存在相同

          err = linq.from(array).select('$.GH').except(linq.from(教工号).select('$.GH').toArray()).toArray()
          linq.from(array).where(e => err.indexOf(e.GH) !== -1).forEach(e => e.error = e.error ? e.error + ';' : '' + '教师基本情况表不存在对应的教工号')
          // 把错误的教师姓名替代掉
          err = linq.from(array).select("$.GH+','+$.XM").except(linq.from(教工号).select("$.GH+','+$.XM").toArray()).toArray()
          linq.from(array).where(e => err.indexOf(e.GH + ',' + e.XM) !== -1)
            .forEach(e => {
              var d = linq.from(教工号).firstOrDefault(item => item.GH == e.GH)
              if (d) {
                e.XM = d.XM
              }
            })
          resolve()
        } catch (e) {
          reject(e)
        }
      })
    },
    async  onChange(model, field, value) {
      //

      if (field == 'GH') {
        if (value == 'error') {
          console.log('error')
          model.GH = ''

          model.XM = ''
        } else {
          var d1 = await get教师基本情况ByGH(this.$root.user.XXID, this.$root.user.CJPC, value)

          console.log('udpate ')
          model.GH = d1[0].GH
          model.XM = d1[0].XM
        }
      }
    },
    async fetchData() {
      var user = getUser()
      var reslut = []

      var datas = await get教师其他情况E(user.XXID, user.CJPC)

      return datas
    },
    onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async(resolve, reject) => {
        console.log(JSON.stringify(temp))
        var model = await add教师其他情况E(temp)
        resolve(model)
      })
    },
    async  bulkAdd(array) {
      await bulkAddJsqtqkE({ JsqtqkE: array })
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async(resolve, reject) => {
        console.log(JSON.stringify(temp))
        var model = await update教师其他情况E(temp)
        resolve(model)
      })
      //
    },
    ondelete(model) {
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async(resolve, reject) => {
        await delete教师其他情况E(temp)
        resolve()
      })
    }
  }
}
</script>
<style>
</style>
